package com.xyw.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xyw.entity.User;
import com.xyw.service.UserBiz;
import com.xyw.service.impl.UserBizImpl;

/**
 * Servlet implementation class AServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest req, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(req, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		HttpSession session=request.getSession();
		ServletContext application=getServletContext();	
		
		String yanzhengma=request.getParameter("uyanzheng");
		String random=session.getAttribute("rand").toString();
		if(random==null || yanzhengma==null || "".equals(random) || "".equals(yanzhengma)) {
			PrintWriter out = response.getWriter();
			out.print("<script>alert('verification code must be filled');history.go(-1);</script>");
			out.flush();
			out.close();
		}
		if(!random.equals(yanzhengma)) {
			PrintWriter out = response.getWriter();
			out.print("<script>alert('Code is incorrect');history.go(-1);</script>");
			out.flush();
			out.close();
		}
		String name=request.getParameter("uname");
		String pwd=request.getParameter("upwd");
		UserBiz biz=new UserBizImpl();
		User user=biz.login(name, pwd);
		if(user!=null) {
			session.setAttribute("user", user);
			
			
			String issave=request.getParameter("isSave");
			if("1".equals(issave)) {
			   Cookie cName=new Cookie("xyw_name", URLEncoder.encode(name));
			   Cookie cPwd=new Cookie("xyw_pwd", pwd);
			   cName.setMaxAge(60*60*24);
			   cPwd.setMaxAge(60*60*24);
			   response.addCookie(cName);
			   response.addCookie(cPwd);
			}
			
			
			
			
			System.out.println("Image size does not match");
			request.setAttribute("user", user);
			if(user.getUserJurisdiction()==2) {
				
				request.getRequestDispatcher("/AdminServlet").forward(request, response);				
			}else if(user.getUserJurisdiction()==1){
				request.getRequestDispatcher("SelectSceneryServlet?op=select1").forward(request, response);
			}else {
									
				request.getRequestDispatcher("/ToIndexServet").forward(request, response);
			}
		}else {
			PrintWriter out = response.getWriter();
			out.print("<script>alert('Login failed');history.go(-1);</script>");
			out.flush();
			out.close();
		}
	}
}